package com.hamster.crm.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crm.mapper.*;
import com.hamster.crm.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定时任务
 * @author Tarry
 * @create 2020/1/18 15:41
 */
@Component
public class Task {
    @Autowired
    private CareerProfitConfigMapper careerProfitConfigMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private CompanyAccountMapper companyAccountMapper;
    @Autowired
    private PersonalMoneyMapper personalMoneyMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private PersonalMessageMapper personalMessageMapper;
    /**
     * 定时结算，招聘人的提成
     */
    @Scheduled(cron = "0 0 0 1 * ? ") //每月1号
    public void recruitment(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为上月第一天
        Date startDate = c.getTime();

        Calendar c2 = Calendar.getInstance();
        c2.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        Date endDate = c2.getTime();

        //获取提成设置
        Double humanAffairs = careerProfitConfigMapper.selectById("1").getHumanAffairs();

        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.ge("join_time",startDate);
        employeeQueryWrapper.lt("join_time",endDate);
        List<Employee> employeeList = employeeMapper.selectList(employeeQueryWrapper);
        for (Employee employee : employeeList) {
            //记录个人流水账
            PersonalMoney personalMoney = new PersonalMoney();
            personalMoney.setTitle("招聘员工提成");
            personalMoney.setCreateTime(new Date());
            personalMoney.setContent("招聘员工："+employee.getName()+"  提成："+humanAffairs);
            personalMoney.setType("1");//0 客户[分公司、合伙人]   1 员工
            personalMoney.setPersonId(employee.getRecruiterId());//个人id【流水账主人】
            personalMoney.setMoney(humanAffairs);
            personalMoneyMapper.insert(personalMoney);

            //给公司总账，添加一条明细记录
            CompanyAccount companyAccount = new CompanyAccount();
            companyAccount.setTitle("招聘员工提成");            //公司明细标题
            companyAccount.setCreateTime(new Date());       //创建时间
            companyAccount.setMoney(-humanAffairs);//收入金额
            companyAccount.setContent("招聘员工："+employee.getName()+"  提成："+humanAffairs);//记账内容
            companyAccountMapper.insert(companyAccount);

            //通知财务一声
            Notice notice = new Notice();
            notice.setContent(employee.getRecruiter()+" 招聘新员工 "+employee.getName());
            noticeMapper.insert(notice);

            PersonalMessage personalMessage = new PersonalMessage();
            personalMessage.setMessageType("通知消息");
            personalMessage.setSenderId("系统");
            personalMessage.setSenderName("系统");
            personalMessage.setMessageLink(notice.getId());
            personalMessage.setCreateTime(new Date());
            personalMessage.setState("0");//消息状态 0未读
            personalMessage.setReceiverId("财务部");
            personalMessage.setReceiverName("财务部");
            personalMessage.setDepartment("财务部");
            personalMessageMapper.insert(personalMessage);

        }

    }
}
